package main

import (
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"sysinfo/syspkg"
	"time"
)

func generateKenerlInfo(ctx *syspkg.SysInfoCtx) error {
	f, err := os.Open("/proc/version")
	if err != nil {
		return err
	}
	kenerlInfo, err := ioutil.ReadAll(f)
	if err != nil {
		return err
	}

	ctx.LinuxInfo = string(kenerlInfo)
	return nil
}

func generateUbuntuInfo(ctx *syspkg.SysInfoCtx) error {
	outputResult, err := exec.Command("/bin/bash", "-c", "lsb_release -a").Output()
	if err != nil {
		return err
	}

	ctx.UbuntuInfo = strings.TrimSpace(string(outputResult))
	return nil
}

func generateTimeInfo(ctx *syspkg.SysInfoCtx) error {
	ctx.CurrentTime = time.Now().Format("2006-01-02 15:04:05")
	return nil
}

func generateWorkInfo(ctx *syspkg.SysInfoCtx) error {
	user, err := exec.Command("/bin/bash", "-c", "whoami").Output()
	if err != nil {
		return err
	}

	ctx.CurrentUser = strings.TrimSpace(string(user))

	path, err := exec.Command("/bin/bash", "-c", "pwd").Output()
	if err != nil {
		return err
	}
	ctx.CurrentPath = strings.TrimSpace(string(path))
	return nil
}

func doChain(ctx *syspkg.SysInfoCtx, functions ...syspkg.GenerateSysInfo) {
	for _, f := range functions {
		f(ctx)
	}
}
